package org.Tools;

import com.google.gson.Gson;
//import org.json.JsonData;
//import org.json.isLoginMsg;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;

public class OnLineOP {

    int port = 10203;    //端口
    String ip = "33.tcp.cpolar.top";    //服务器ip
    //json数据处理工具
    Gson gson = new Gson();

    public ArrayList<String> send(String msg) throws IOException {
        //返回结果
        String ans;
        //返回的字符串序列
        ArrayList<String> list = new ArrayList<>();

        //创建连接
        Socket socket = new Socket(ip,port);
        System.out.println("连接到远程主机："+socket.getRemoteSocketAddress());
        //创建输出流
        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
        //输入流
        DataInputStream in = new DataInputStream(socket.getInputStream());

//        try {
//            Thread.sleep(3000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }

        //向服务器发送请求
        out.writeUTF(msg);

        //获取字符串的个数
        String count = in.readUTF();
        int cnt = Integer.parseInt(count);

        for(int i = 0; i < cnt; i++) {
            //接受服务器返回结果
            ans=in.readUTF();
            list.add(ans);
        }


        //释放资源
        in.close();
        out.close();
        socket.close();

        return list;
    }

    //文件下载
    public void fileDownload(String msg) throws IOException {

        Socket socket = new Socket("127.0.0.1",3456);
        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
        DataInputStream in = new DataInputStream(socket.getInputStream());
        out.writeUTF(msg);
        //文件输出
        FileOutputStream fileOutputStream = new FileOutputStream("01.png");
        byte[] buffer = new byte[1024];
        int bytesRead;  //-1传输结束
        while ((bytesRead = in.read(buffer))!= -1) {
            fileOutputStream.write(buffer, 0, bytesRead);
        }
        fileOutputStream.close();
        in.close();
        out.close();
        socket.close();
    }

    //文件上传
    public void flieUpload(String msg,File file) throws IOException {

        Socket socket = new Socket("127.0.0.1",3456);
        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
        DataInputStream in = new DataInputStream(socket.getInputStream());
        out.writeUTF(msg);
        //输入文件地址
        FileInputStream fileInputStream = new FileInputStream(file.getAbsoluteFile());
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = fileInputStream.read(buffer))!= -1) {
            out.write(buffer, 0, bytesRead);
        }

    }

    //登陆监测
//    public isLoginMsg isLogin(JsonData Lmsg) throws IOException {
//        //返回结果
//        isLoginMsg ans;
//        //发送数据
//        String sendMsg = null;
//        //转化Lmsg为字符串
//        sendMsg = gson.toJson(Lmsg);
//        //接收反馈的字符串序列
//        ArrayList<String> list = send(sendMsg);
//        //调用send方法向服务器发出请求并收回结果
//        String Jmsg = list.get(0);
//        //创建Gson类
//        Gson gson = new Gson();
//        //转化结果
//        ans=gson.fromJson(Jmsg,isLoginMsg.class);
//
//        return ans;
//    }

    //单字符串传输
    public String sendone(String msg) throws IOException {
        //返回结果
        String ans;

        //创建连接
        Socket socket = new Socket(ip,port);
        System.out.println("连接到远程主机："+socket.getRemoteSocketAddress());
        //创建输出流
        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
        //输入流
        DataInputStream in = new DataInputStream(socket.getInputStream());

//        try {
//            Thread.sleep(3000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }

        //向服务器发送请求
        out.writeUTF(msg);

        //获取字符串的个数
        ans = in.readUTF();

        //释放资源
        in.close();
        out.close();
        socket.close();

        return ans;
    }

}
